import Taro, { Component } from '@tarojs/taro'
import { View, Block } from '@tarojs/components';
import { get as getGlobalData } from '@/global'
import './index.scss';
import MessageItem from '@/components/messageItem/index';
import http from '@/utils/http';
import StatusPages from '@/components/statusPages/index';
import MoreFooter from '@/components/moreFooter/index';

export default class MessageList extends Component {
    constructor() {
        super();
        this.state = {
            pagenumber: 1,
            noticeList: [],
            moreType: 'none',
            statusType: 'none', // none 不显示空白页  empty //显示空白页
            statuserro: false, // 是否显示错误页
        }
    }
    componentWillMount() {

    }
    componentDidShow() {
        let userInfo = getGlobalData('userInfo') || utils.getUserInfo();
        this.setState({
            userInfo,
            pagenumber: 1
        }, () => {
            this.getNotice()
        })
    }
    config = {
        navigationBarTitleText: '消息中心'
    }
    getNotice() {
        let { userInfo, noticeList, moreType, pagenumber } = this.state;
        http.get('getNotice', {
            userid: userInfo.id,
            pagesize: 10,
            pagenumber: pagenumber
        }).then((res) => {
            console.log(res);
            Taro.stopPullDownRefresh();
            let list = res.datas.pageRes || [];
            list.map((item) => {
                item.timesString = this.timesFun(item.createtime.replace(/-/g, "/"))
            })
            moreType = "showText";
            list.length < 10 ? moreType = "noMore" : moreType;
            if (pagenumber != 1) {
                list = noticeList.concat(list);
            }
            this.setState({
                moreType,
                statusType: list.length > 0 ? 'none' : 'empty',
                statuserro: false,
                noticeList: list
            });
        }).catch((e) => {
            console.log(e);
            Taro.stopPullDownRefresh();
            this.setState({
                moreType: noticeList.length > 0 ? 'error' : 'none',
                statusType: noticeList.length > 0 ? 'none' : 'empty',
                statuserro: noticeList.length > 0 ? false : true,
            })
        })
    }
    refresh() { //下拉刷新
        var {
            moreType
        } = this.state;
        if (moreType != "loading") {
            this.setState({
                pagenumber: 1,
                moreType: 'none'
            }, () => {
                this.getNotice();
            })
        }
    }
    onPullDownRefresh() {
        this.refresh();
    }
    onReachBottom() { //滚动触底加载
        let {
            moreType,
            pagenumber,
        } = this.state;
        if (moreType == "showText") {
            pagenumber = pagenumber + 1;
            this.setState({
                pagenumber,
                moreType: "loading"
            }, () => {
                this.getNotice();
            });
        }
    }
    timesFun(timesData) {
        var dateBegin = new Date(timesData);
        var dateEnd = new Date(); //获取当前时间
        var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
        var leave1 = dateDiff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
        //计算相差秒数
        var timesString = '';
        if (dayDiff != 0) {
            timesString = timesData.substring(0, timesData.length - 3);
        } else if (dayDiff == 0 && hours != 0) {
            var current_times = timesData.split(' ')[1].substring(0, 5);
            timesString = (dateEnd.getHours() < hours ? '昨天' : '今天') + current_times;
        } else if (dayDiff == 0 && hours == 0) {
            timesString = '刚刚';
            if (minutes) {
                timesString = minutes + '分钟之前';
            }
        }
        return timesString
    }
    render() {
        const { statuserro, statusType, moreType, pagenumber, noticeList } = this.state;
        return (
            <View className='message-list pl-2-5 pr-2-5 box_s ov_h'>
                <Block>
                    {
                        noticeList.map((item) => {
                            return (
                                <MessageItem key={item.id} messageItem={item} />
                            )
                        })
                    }
                    <MoreFooter moreType={moreType} restloading={() => { this.setState({ pagenumber: pagenumber > 1 ? pagenumber - 1 : 1 }, () => { this.getNotice() }) }} ></MoreFooter>
                </Block>
                <StatusPages mt='mt-20'
                    refresh={() => { this.refresh() }}
                    statusType={statusType}
                    statuserro={statuserro}
                ></StatusPages>
            </View>
        )
    }
}

